//
//  HMWelcomeViewController.swift
//  webo
//
//  Created by apple on 2016/11/15.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HMWelcomeViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        sartAnimation()
    }
    
    func sartAnimation(){
        let offsetY = 120 + iconView.bounds.height - self.view.bounds.height
       
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 7, options: .curveEaseIn, animations: {
            self.iconView.snp.updateConstraints { (make) in
                make.bottom.equalTo(self.view).offset(offsetY)
            }
            self.view.layoutIfNeeded()
        }, completion: {(isYes: Bool) -> () in
        
//            UIView.animate(withDuration: 0.5, animations: { 
//                self.welcomeLb.alpha = 1
//                UIApplication.shared.keyWindow?.rootViewController = HMTabBarController()
            //            })
            UIView.animate(withDuration: 0.5, animations: {
                self.welcomeLb.alpha = 1
                
            }, completion: { (isyes) in
//                UIApplication.shared.keyWindow?.rootViewController = HMTabBarController()
            NotificationCenter.default.post(name: NSNotification.Name(chooseNotificationLoginOn), object: nil)
            })
            
        })

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        setupUI()
    }

    func setupUI(){
        self.view.addSubview(self.iconView)
        self.view.addSubview(self.welcomeLb)
        self.iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-120)
        }
        
        self.welcomeLb.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(20)
        }
       welcomeLb.alpha = 0
        iconView.layer.cornerRadius = 42.5
        iconView.clipsToBounds = true
        
        iconView.sd_setImage(with: HMUserAccountViewModel.shared.headUrl)
        
    }
    
    lazy var iconView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    lazy var welcomeLb:UILabel = UILabel(text: "欢迎归来", fontSize: 14, numbLines: 0, tcolor: UIColor.darkGray)
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
